<template>
  <div class="app-container">
    <el-tag color="#2b85e4" style="color:white;margin-bottom:20px" size="large">
      模块信息
    </el-tag>
    <el-row :gutter="30">
      <el-col :span="24">
        <el-table
          :data="moduleTableData"
          border
          empty-text="暂无数据"
          style="width: 100%">
          <el-table-column
            label="模块编码"
            property="code"
            width="200">
          </el-table-column>
          <el-table-column
            label="模块名称"
            property="name"
            width="150">
          </el-table-column>
          <el-table-column
            label="模块描述"
            property="url">
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <br/>
    <br/>
    <el-tag color="#2b85e4" style="color:white;margin-bottom:20px" size="large">
      操作列表
    </el-tag>
    <el-row :gutter="30">
      <el-col :span="24">
        <div>
          <el-button icon="el-icon-plus" type="primary" size="mini" @click="addData">新增</el-button>
          <el-button icon="el-icon-close" type="danger" size="mini" @click="deleteAny">删除</el-button>
        </div>
        <div class="query-form">
          <el-form ref="tableSearchParam" :model="tableSearchParam" label-width="50px" :inline="true">
            <el-form-item label="操作编码" label-width="70px">
              <el-input v-model="tableSearchParam.code" style="width:200px" size="small" clearable></el-input>
            </el-form-item>
            <el-form-item label="操作名称" label-width="90px">
              <el-input v-model="tableSearchParam.name" style="width:200px" size="small" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="listTableData" icon="el-icon-search" size="small">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <br/>
        <el-table
          :data="tableData"
          border
          empty-text="暂无数据"
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="35">
          </el-table-column>
          <el-table-column
            label="操作编码"
            property="code"
            width="230">
          </el-table-column>
          <el-table-column
            label="操作名称"
            property="name"
            width="190">
          </el-table-column>
          <el-table-column
            label="访问路径"
            property="url"
            width="235">
          </el-table-column>
          <el-table-column
            property="createdTime"
            label="创建时间"
            width="140">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                icon="el-icon-edit"
                type="success"
                @click="editData(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-close"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page=this.currentPage
            :page-sizes="[10, 20, 30, 40]"
            :page-size=this.pageSize
            layout="total, sizes, prev, pager, next, jumper"
            :total=this.total>
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="actionType=='add'?'新增操作':'修改操作'" :visible.sync="dialogFormVisible" top="10vh">
      <el-form :model="formData" style="width:400px" :rules="operationRules" ref="operationRuleForm">
        <el-form-item label="操作编码" :label-width="formLabelWidth" prop="code">
          <el-input v-model="formData.code" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="操作名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="formData.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序码" :label-width="formLabelWidth">
          <el-input v-model="formData.sort" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="操作url" :label-width="formLabelWidth" prop="url">
          <el-input v-model="formData.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="关联操作" :label-width="formLabelWidth">
          <el-select v-model="formData.relateOperationId" placeholder="请选择" no-data-text="暂无数据" multiple
                     style="width: 320px" filterable>
            <el-option
              v-for="item in allOperationData"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可见" :label-width="formLabelWidth">
          <el-switch
            v-model="formData.visiable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="操作描述" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="formData.description" auto-complete="off" :rows="6"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd" v-if="actionType=='add'" :loading="loading">保存</el-button>
        <el-button type="primary" @click="handleEdit" v-else :loading="loading">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss">
  .page {
    text-align: center;
    margin-top: 20px;
  }

  .query-form {
    border-bottom: 1px solid #ebeef5;
    padding-top: 25px;
  }
</style>

<script>
  import moduleApi from '@/api/moduleApi'
  import operationApi from '@/api/operationApi'

  export default {
    created() {
      this.getModuleData()
      this.initData()
    },
    data() {
      return {
        allOperationData: [],
        moduleTableData: [],
        tableData: [],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        tableSearchParam: {
          code: '',
          name: ''
        },
        moduleId: this.$route.query.id,
        dialogFormVisible: false,
        formLabelWidth: '80px',
        formData: {
          code: '',
          name: '',
          sort:'',
          url: '',
          relateOperationId: [],
          description: '',
          visiable: '1'

        },
        loading: false,
        actionType: '',
        multipleSelection: [],
        operationRules: {
          code: [
            {required: true, message: '请输入操作编码', trigger: 'blur'},
            {max: 50, message: '长度不超过50 个字符', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入操作名称', trigger: 'blur'},
            {max: 50, message: '长度不超过50 个字符', trigger: 'blur'}
          ],
          url: [
            {required: true, message: '请输入操作url', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      initData() {
        this.listTableData()
      },
      listTableData() {
        operationApi.listOperation(this.moduleId, this.tableSearchParam, this.currentPage, this.pageSize, (res) => {
          this.tableData = res.list
          this.total = res.total
        }, () => {
        })
      },
      getModuleData() {
        this.moduleTableData = []
        moduleApi.getModuleById(this.moduleId, (res) => {
          this.moduleTableData.push(res)
        }, () => {
        })
      },
      addData() {
        //清空历史值
        this.formData = {
          code: '',
          name: '',
          sort:'99',
          url: '',
          description: '',
          visiable: '1'
        }
        this.allOperationData = []
        operationApi.listAllOperation(null, (res) => {
          this.allOperationData = res
          this.formData.moduleId = this.moduleId
          this.actionType = 'add'
          this.dialogFormVisible = true
        }, () => {
          this.$refs['operationRuleForm'].resetFields()
        })
      },
      handleAdd() {
        this.$refs['operationRuleForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.formData.relateOperationId = (this.formData.relateOperationId != null && this.formData.relateOperationId != '') ? this.formData.relateOperationId.join(',') : ''
            operationApi.addOperation(this.formData, (res) => {
              this.loading = false
              this.dialogFormVisible = false
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.initData()
            }, () => {
              this.loading = false
            })
          }
        })

      },
      editData(index, row) {
        //清空历史值
        this.formData = {
          code: '',
          name: '',
          sort:'',
          url: '',
          description: '',
          visiable: '1'

        }
        let that = this
        that.actionType = 'edit'
        operationApi.listAllOperation(null, (res) => {
          this.allOperationData = res
          operationApi.getOperationById(row.id, (res1) => {
            if (res1.relateOperationId == null || res1.relateOperationId == '') {
              res1.relateOperationId = []
            } else {
              res1.relateOperationId = res1.relateOperationId.split(',')
            }
            that.formData = res1
            that.formData.id = row.id
            that.dialogFormVisible = true
          }, () => {

          })
        }, () => {
        })
      },
      handleEdit() {
        this.$refs['operationRuleForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.formData.relateOperationId = this.formData.relateOperationId.join(',')
            operationApi.updateOperation(this.formData.id, this.formData, (res) => {
              this.loading = false
              this.dialogFormVisible = false
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.initData()
            }, () => {
              this.loading = false
            })
          }
        })
      },
      handleDelete(index, row) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          operationApi.deleteSingleOperation(row.id, (res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }, () => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.listTableData()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.listTableData()
      },
      searchTableData() {

      },
      handleSelectionChange(val) {
        this.multipleSelection = []
        for (var ms of val) {
          this.multipleSelection.push(ms.id)
        }
      },
      deleteAny() {
        if (this.multipleSelection.length == 0) {
          this.$message({
            type: 'warning',
            message: '请选择记录!'
          })
          return;
        }
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          operationApi.deleteOperation(this.multipleSelection, (res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }, () => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      reloadPermission() {
      }
    }
  }
</script>
